{extend name="../../../view/public/base" /}

{block name="css"}
<style>
    body {
        background-color: #F2F2F2;
    }
</style>
{/block}

{block name="body"}
<div class="bw-container">
    <div class="bw-main">
    <div class="layui-row layui-col-space15">
        <div class="layui-col-md12">

            <!--搜索区域-->
            <!--<div class="layui-card">
                <div class="layui-collapse" lay-accordion="" lay-filter="search">
                    <div class="layui-colla-item">
                        <h2 class="layui-colla-title">查询条件</h2>
                        <div class="layui-colla-content">
                            <form id="searchForm" class="layui-form" lay-filter="searchForm" autocomplete="off">

                                <div class="layui-form-item layui-inline">
                                    <label class="layui-form-label">ID</label>
                                    <div class="layui-input-inline">
                                        <input type="number" name="id" placeholder="请输入ID"
                                               class="layui-input">
                                    </div>
                                </div>

                                <div class="layui-form-item">
                                    <div class="layui-input-block">
                                        <button class="layui-btn" type="button" lay-submit lay-filter="searchBtn">
                                            搜索
                                        </button>
                                        <button class="layui-btn layui-btn-primary" type="button" onclick="resetForm()">
                                            重置
                                        </button>
                                    </div>
                                </div>
                            </form>
                        </div>
                    </div>
                </div>
            </div>-->

            <!--列表区域-->
            <div class="layui-card" style="background-color: #F2F2F2;">
                <table id="list" lay-filter="listFilter"></table>

                <!--工具栏-->
                <script type="text/html" id="listToolbar">
                    <!--<a class="layui-btn layui-btn-sm layui-btn-normal" lay-event="refresh" title="刷新">刷新</a>
                    {Auth:node name='/manage/admin.Plugin/add'}
                    <a class="layui-btn layui-btn-sm" lay-event="add" title="新增">新增</a>
                    {/Auth:node}
                    {Auth:node name='/manage/admin.Plugin/del'}
                    <a class="layui-btn layui-btn-sm layui-btn-danger" lay-event="del" title="删除">删除</a>
                    {/Auth:node}-->
                </script>

                <!--标识-->
                <script type="text/html" id="name">
                    {{ d.name }}
                </script>
                <!--名称-->
                <script type="text/html" id="title">
                    {{ d.title }}
                </script>
                <!--作者-->
                <script type="text/html" id="author">
                    {{ d.author }}
                </script>
                <!--版本号-->
                <script type="text/html" id="version">
                    {{ d.version }}
                </script>
                <!--状态:-1=未安装,0=禁用,1=启用-->
                <script type="text/html" id="status">
                    {{# if (d.status == '-1') { }}
                    <span style="color: #1E9FFF;">未安装</span>
                    {{# } else if(d.status == '0') { }}
                    <span style="color: #1E9FFF;">禁用</span>
                    {{# } else if(d.status == '1') { }}
                    <span style="color: #1E9FFF;">启用</span>
                    {{# } else { }}
                    <span style="color: #1E9FFF;">{{d.status}}</span>
                    {{# } }}
                </script>
                <!--安装时间-->
                <script type="text/html" id="create_time">
                    {{ formatDate(d.create_time) }}
                </script>
                <!--更新时间-->
                <script type="text/html" id="update_time">
                    {{ formatDate(d.update_time) }}
                </script>


                <!--状态开关-->
                <script type="text/html" id="listBar">
<!--                    <a class="layui-btn layui-btn-xs layui-btn-danger" lay-event="del">删除</a>-->

                    {{# if (d.status == '-1') { }}
                    {Auth:node name='/manage/admin.Plugin/install'}
                    <a class="layui-btn layui-btn-xs" lay-event="install">安装</a>
                    {/Auth:node}
                    {{# } else if(d.status == '0') { }}
                    <a class="layui-btn layui-btn-xs" lay-event="edit">价格</a>
                    {Auth:node name='/manage/admin.Plugin/enable'}
                    <a class="layui-btn layui-btn-xs" lay-event="enable">启用</a>
                    {/Auth:node}
                    {Auth:node name='/manage/admin.Plugin/uninstall'}
                    <a class="layui-btn layui-btn-xs" lay-event="uninstall">卸载</a>
                    {/Auth:node}
                    {{# } else if(d.status == '1') { }}
                    <a class="layui-btn layui-btn-xs" lay-event="edit">价格</a>
                    {Auth:node name='/manage/admin.Plugin/config'}
                    <a class="layui-btn layui-btn-xs" lay-event="config">插件配置</a>
                    {/Auth:node}
                    {Auth:node name='/manage/admin.Plugin/disable'}
                    <a class="layui-btn layui-btn-xs" lay-event="disable">禁用</a>
                    {/Auth:node}
                    {{# } else { }}
                    {{# } }}
                </script>
            </div>
        </div>
    </div>
    </div>
</div>

{/block}

{block name="js"}
<script>
    var element, layer, form, table, bwajax, $;

    layui.use(['element', 'layer', 'form', 'table', 'bwajax'], function () {
        element = layui.element;
        layer = layui.layer;
        form = layui.form;
        table = layui.table;
        bwajax = layui.bwajax.instance();
        $ = layui.jquery;

        //执行列表
        table.render({
            elem: '#list',
            url: "{:Url('index')}",
            response: {
                statusCode: 200,  //规定成功的状态码，默认：0
            },
            parseData: function (res) { //res 即为原始返回的数据
                return {
                    "code": res.code, //解析接口状态
                    "msg": res.msg, //解析提示文本
                    "count": res.data.data.total, //解析数据长度
                    "data": res.data.data.list //解析数据列表
                };
            },
            // toolbar: '#listToolbar',
            cols: [[
                // {type:'checkbox', fixed: 'left'},
                {field: 'id', title: 'ID', align: 'center', width: 80, fixed: 'left'},
                {field: 'name', title: '标识', align: 'center', minWidth: 150, templet: '#name'},
                {field: 'title', title: '名称', align: 'center', minWidth: 150, templet: '#title'},
                {field: 'author', title: '作者', align: 'center', minWidth: 150, templet: '#author'},
                {field: 'price', title: '价钱', align: 'center', minWidth: 100},
                {field: 'version', title: '版本号', align: 'center', minWidth: 150, templet: '#version'},
                {field: 'status', title: '状态', align: 'center', width: 100, templet: '#status'},
                {field: 'create_time', title: '安装时间', align: 'center', width: 160, templet: '#create_time'},
                {field: 'update_time', title: '更新时间', align: 'center', width: 160, templet: '#update_time'},

                {title: '操作', toolbar: '#listBar', align: 'center', fixed: "right", width: 180}
            ]],
            limits: [10, 15, 20, 25, 50, 100],
            limit: 10,
            // page: true
        });

        //监听表格头部工具栏事件
        table.on('toolbar(listFilter)', function (obj) {
            var checkStatus = table.checkStatus(obj.config.id);
            switch (obj.event) {
                case 'refresh':
                    reload();
                    break;
                case 'add':
                    layer.open({
                        title: '新增',
                        type: 2,
                        shade: 0.2,
                        maxmin: true,
                        area: ['50%', '90%'],
                        content: "{:Url('add')}",
                    });
                    break;
                case 'del':
                    var checkStatus = table.checkStatus('list');
                    var data = checkStatus.data;

                    var idsArr = [];
                    data.forEach(function(value) {
                        idsArr.push(value.id);
                    });
                    ids = idsArr.join(',');

                    if(!ids) layer.msg('没有选中行', {icon: 2});
                    else del(ids);

                    break;
            }
        });

        //监听工具条
        table.on('tool(listFilter)', function (obj) { //注：tool 是工具条事件名，test 是 table 原始容器的属性 lay-filter="对应的值"
            var data = obj.data; //获得当前行数据
            var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）

            switch (layEvent) {
                case 'edit':
                    layer.open({
                        title: '价格',
                        type: 2,
                        shade: 0.2,
                        maxmin: true,
                        area: ['50%', '90%'],
                        content: "{:Url('edit')}?id=" + data.id,
                    });
                    break;
                case 'del':
                    del(obj.data.id);
                    break;
                case 'install'://安装
                    install(obj.data.name);
                    break;
                case 'uninstall'://卸载
                    uninstall(obj.data.name);
                    break;
                case 'enable'://启用
                    enable(obj.data.name);
                    break;
                case 'disable'://禁用
                    disable(obj.data.name);
                    break;
                case 'config'://修改插件配置
                    config(obj.data.name);
                    break;
            }
        });

        form.on('submit(searchBtn)', function (data) {
            reload(1);

            return false;
        })


    });

    //表格重载
    function reload(page) {
        var where = getWhere();//获取搜索条件

        var page_now = $(".layui-laypage-em").next().html(); //当前页码值
        var page_to = page ? page : page_now;

        table.reload('list', {
            where: where, //设定异步数据接口的额外参数
            // page: {curr: page_to}//设置页数
        });
    }

    //安装
    function install(name) {
        layer.confirm('确认安装?', function (index) {
            //ajax调用后台接口
            bwajax.post("{:Url('install')}?name=" + name)
                .then(function (response) {
                    if (response.data.data.errcode === 0) {
                        layer.msg('安装成功', {icon: 1});
                        reload();
                    } else {
                        layer.msg(response.data.msg, {icon: 2});
                    }
                });
            layer.close(index);
        });
    }

    //卸载
    function uninstall(name) {
        layer.confirm('确认卸载?', function (index) {
            //ajax调用后台接口
            bwajax.post("{:Url('uninstall')}?name=" + name)
                .then(function (response) {
                    if (response.data.data.errcode === 0) {
                        layer.msg('卸载成功', {icon: 1});
                        reload();
                    } else {
                        layer.msg(response.data.msg, {icon: 2});
                    }
                });
            layer.close(index);
        });
    }

    //启用
    function enable(name) {
        layer.confirm('确认启用?', function (index) {
            //ajax调用后台接口
            bwajax.post("{:Url('enable')}?name=" + name)
                .then(function (response) {
                    if (response.data.data.errcode === 0) {
                        layer.msg('启用成功', {icon: 1});
                        reload();
                    } else {
                        layer.msg(response.data.msg, {icon: 2});
                    }
                });
            layer.close(index);
        });
    }

    //禁用
    function disable(name) {
        layer.confirm('确认禁用?', function (index) {
            //ajax调用后台接口
            bwajax.post("{:Url('disable')}?name=" + name)
                .then(function (response) {
                    if (response.data.data.errcode === 0) {
                        layer.msg('禁用成功', {icon: 1});
                        reload();
                    } else {
                        layer.msg(response.data.msg, {icon: 2});
                    }
                });
            layer.close(index);
        });
    }

    //修改插件配置
    function config(name) {
        layer.open({
            title: '修改插件配置',
            type: 2,
            shade: 0.2,
            maxmin: true,
            area: ['50%', '90%'],
            content: "{:Url('config')}?name=" + name,
        });
    }
</script>
{/block}